import Vue from 'vue'
import template from './BlocklyEditor.vue'

class BlocklyEditor {
  constructor () {
    this.instance = null
  }

  static _init () {
    if (!this.instance) {
      const Profile  = Vue.extend(template)
      this.instance = new Profile({
        el: document.createElement('div')
      })
      document.body.appendChild(this.instance.$el)
    }
  }
  
  static open (options) {
    this._init()
    return this.instance.open(options)
  }

  /**
   * desktop快捷入口
   * @param {*} data 
   */
  static openForDesktop(data) {
    this.open({
      data,
      callback: (payload) => {
        const { funcDesc, javascript, xml } = payload;
        data.funcDesc = funcDesc;
        data.xml = xml;
        data.javascript = javascript;
        data.eventName = funcDesc;
        data.eventXml = xml;
        data.eventJs = javascript;
      },
    });
  }
}

export default BlocklyEditor